
$(function() {
	loadFollowQuestionList();
	
	$(document).ready(function() {  
		  
		var MaxInputs       = 30; //maximum input boxes allowed  
		var InputsWrapper   = $("#InputsWrapper"); //Input boxes wrapper ID  
		var AddButton       = $("#AddMoreFileBox"); //Add button ID  
		  
		var x = InputsWrapper.length; //initlal text box count  
		var FieldCount=1; //to keep track of text box added  
		  
		$(AddButton).click(function (e)  //on add input button click  
		{  
		        if(x <= MaxInputs) //max input box allowed  
		        {  
		            FieldCount++; //text box added increment  
		            //add input box  
		            $(InputsWrapper).append('<div class="form-group"><label class="col-sm-3 control-label">最小分值：</label><div class="col-sm-8"><input name="grade['+ FieldCount +'].minGrade" class="form-control" placeholder="分值 '+ FieldCount +'" type="text" data-rule-required="true" data-rule-digits="true" /></div><label class="col-sm-3 control-label">最大分值：</label><div class="col-sm-8"><input name="grade['+ FieldCount +'].maxGrade" class="form-control" placeholder="分值 '+ FieldCount +'" type="text" data-rule-required="true" data-rule-digits="true" /></div><label class="col-sm-3 control-label">显示结果：</label><div class="col-sm-8"><input name="grade['+ FieldCount +'].content" class="form-control" placeholder="结果 '+ FieldCount +'" type="text" data-rule-required="true" /></div><label class="col-sm-3 control-label">描述内容：</label><div class="col-sm-8" id="IntegralContentWrite"><textarea type="text" name="grade['+ FieldCount +'].remark" rows="10" cols="60" data-rule-required="true"></textarea></div><a href="#" class="removeclass">删除</a></div>');  
		            x++; //text box increment  
		        }  
		return false;  
		});  
		  
		$("body").on("click",".removeclass", function(e){ //user click on remove text  
		        if( x > 1 ) {  
		                $(this).parent('div').remove(); //remove text box  
		                x--; //decrement textbox  
		        }  
		return false;  
		})   
		  
		});  
	
	$("#IntegralView").validate(
			{
				submitHandler : function(form) {
					var options = {
						url : "saveGradeView",
						type : "post",
						dataType : "json",
						success : function(data) {
							if (data.success) {
								$.success('操作成功',function() {
									layer.close(pageIndex);
									search();
								});
							} else {
								$.error(data.msg);
							}
						},
						error : function(XMLHttpRequest, textStatus,
										 errorThrown) {
							$.warn('服务忙,请稍后再试.');
						}
					};
					$(form).ajaxSubmit(options);
				},
				invalidHandler : function(form, validator) {

					return false;
				}

			});
})

function loadFollowQuestionList() {
	$('#followQuestionCategoryListTable').bootstrapTable({
		method : 'post',
		url : "getFollowQuestionCListData",
		height : $(window).height() - 100,
		striped : true,
		minimumCountColumns : 2,
		smartDisplay : true,
		toolbar : "#followQuestionCategoryListTableToolbar",
		pagination : true,
		sidePagination : "server",
		pageNumber : 1,
		pageSize : 10,
		pageList : [ 10, 20, 30 ],
		queryParamsType : "limit",
		queryParams:queryParams,
		contentType : "application/x-www-form-urlencoded",
		columns : [{
			field : 'id',
			title : 'ID',
			visible : false
		}, {
			title : '序号',
			align : 'center',
			width : '3%',
			formatter : serilNumFormatter
		},  {
			field : 'title',
			title : '名称',
			align : 'center',
			width : '10%'
		},  {
			field : 'parentTitle',
			title : '父级名称',
			align : 'center',
			width : '10%'
		},{
			field : 'content',
			title : '描述',
			align : 'center',
			width : '30%'
		}, {
			field : 'createTime',
			title : '创建时间',
			align : 'center',
			width : '10%'
		},{
			field : 'answerNumber',
			title : '答题人数',
			align : 'center',
			width : '5%'
		}, {
			field : 'status',
			title : '状态',
			align : 'center',
			width : '5%'
		}, {
			field : 'questionLevel',
			title : '级别',
			align : 'center',
			width : '5%'
		},{
			title : '操作',
			align : 'center',
			width : '10%',
			valign : 'middle',
			formatter : operateFormatter
		}]
	});

}

function search() {
	$('#followQuestionCategoryListTable').bootstrapTable('destroy');
	loadFollowQuestionList();
}

function queryParams(params) {
	return {
		title :$("input[name='title']").val(),
		status:$("select[name='status']").val(),
		questionLevel:$("select[name='questionLevel']").val(),
		limit:params.limit,
		offset:params.offset
	}
}

function operateFormatter(value, row, index) {
	
	if (row.status =='发布') { 
		return[
		       '<a class="ok"  href="javascript:void(0)" onClick="Integral('+index+')" title="积分"><i class="glyphicon glyphicon-ok">积分</i></a>',
	           '<a class="search"  href="getFollowQuestionCategoryView?id='+row.id+'" title="编辑"><i class="glyphicon glyphicon-edit">编辑</i></a>',
	           '<a class="remove" href="javascript:void(0)" onClick="del('+ index +')" title="删除"><i class="glyphicon glyphicon-remove">删除</i></a>',
	           '<a class="edit"  href="javascript:void(0)" onClick="issue('+ index+ ')" title="取消发布"><i class="glyphicon glyphicon-export">取消发布</i></a>'
	           ].join('');
	}
	return [
        	'<a class="ok"  href="javascript:void(0)" onClick="Integral('+index+')" title="积分"><i class="glyphicon glyphicon-ok">积分</i></a>',
        	'<a class="search"  href="getFollowQuestionCategoryView?id='+row.id+'" title="编辑"><i class="glyphicon glyphicon-edit">编辑</i></a>',
        	'<a class="remove" href="javascript:void(0)" onClick="del('+ index +')" title="删除"><i class="glyphicon glyphicon-remove">删除</i></a>',
    		'<a class="edit"  href="javascript:void(0)" onClick="issue('+ index+ ')" title="发布"><i class="glyphicon glyphicon-export">发布</i></a>',
        	]
		.join('');
}

function del(index) {
	var data = $('#followQuestionCategoryListTable').bootstrapTable('getData');
	var id = data[index].id;
	var sta = data[index].status;
	var status;
	if(sta == '不显示'){
		status = 1;
	}else if (sta == '显示') {
		status = 0;
	}
	else{
		status = -1;
	}
	layer.confirm("确认删除吗?", {
		btn : [ '确定', '取消' ]
	}, function() {
		$.getJSON("getDel", {
			id : id,
			status : -1
		}, function(data) {
			if (data.success) {
				$.success("操作成功", function() {
					search();
				});
			}else{
				$.error(data.msg);
			}

		});
	}, function() {
		$.warn("您取消了操作");
	});
}

function Integral(index){
	var data = $('#followQuestionCategoryListTable').bootstrapTable('getData');
	$("#handleId").val(data[index].id);
	$("#IntegralContentWrite").css("display","");
	$("#btnStatus").css("display","");
	pageIndex = layer.open({
		type : 1,
		title : '积分',
		shadeClose : true,
		shade : 0.5,
		area : [ '800px', '600px' ],
		content : $('#handleIntegral')
	});
}	

function issue(index) {
	var data = $('#followQuestionCategoryListTable').bootstrapTable('getData');
	var id = data[index].id;
	var categoryId = data[index].categoryId;

	var sta = data[index].status;
	var status;
	if(sta == '发布'){
		status = 1;//不发布
		statusStr = "取消发布";
	}else if (sta == '未发布') {
		status = 0;
		statusStr = "发布";
	}
	layer.confirm("确认"+statusStr+"吗?", {
		btn : [ '确定', '取消' ]
	}, function() {
		$.getJSON("getIssue", {
			id : id,
			status : status,
			categoryId:categoryId
		}, function(data) {
			if (data.success) {
				$.success("操作成功", function() {
					search();
				});
			}else{
				$.error(data.msg);
			}

		});
	}, function() {
		$.warn("您取消了操作");
	});
}